﻿#include "mymessagebox.h"
#include <QMessageBox>
#include <QEventLoop>
#include <QApplication>
#include <QTimer>

MyMessageBox::MyMessageBox(QObject *parent) : QObject(parent)
{

}

MyMessageBox::MyMessageBox(const QString &title, const QString &message):
    m_title(title),
    m_message(message)
{

}

void MyMessageBox::warning(const QString &title, const QString &message){
    QEventLoop eventLoop;
    auto messageBox = new MyMessageBox(title, message);
    connect(messageBox, SIGNAL(destroyed()), &eventLoop, SLOT(quit()));
    messageBox->readyShow(0);
    eventLoop.exec();
}

void MyMessageBox::information(const QString &title, const QString message){
    QEventLoop eventLoop;
    auto messageBox = new MyMessageBox(title, message);
    connect(messageBox, SIGNAL(destroyed()), &eventLoop, SLOT(quit()));
    messageBox->readyShow(1);
    eventLoop.exec();
}

void MyMessageBox::readyShow(int type)
{
    this->moveToThread(qApp->thread());
    if(type == 0){
        QTimer::singleShot(0, this, SLOT(onShowWarining()));
    }else{
        QTimer::singleShot(0, this, SLOT(onShowInformation()));
    }
}

void MyMessageBox::onShowWarining(void){
    QMessageBox::warning(nullptr, m_title, m_message);
    this->deleteLater();
}

void MyMessageBox::onShowInformation(void){
    QMessageBox::information(nullptr, m_title, m_message);
    this->deleteLater();
}
